/**
 * 城市助手主键表
 */
package com.mall.service.commkey;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.commkey.CommKeyService;
import com.mall.custom.dao.read.CommkeyReadMapper;
import com.mall.custom.dao.write.CommkeyWriteMapper;
import com.mall.enums.RegionEnum;
import com.mall.pojo.Commkey;
import com.mall.pojo.CommkeyExample;
import com.mall.pojo.CommkeyExample.Criteria;

@Service
public class CommkeyServiceImpl implements CommKeyService {

	@Autowired
	private CommkeyReadMapper commkeyReadMapper;
	@Autowired
	private CommkeyWriteMapper commkeyWriteMapper;

	@Override
	public List<Commkey> getCommKeyByExamPle(CommkeyExample example) {

		return this.commkeyReadMapper.selectByExample(example);
	}

	/**
	 * 根据commkeyId获取ComKey对象
	 */
	public List<Commkey> getComKeyByCommOnlyKey(Integer commkeyId) {
		CommkeyExample example = new CommkeyExample();
		Criteria criteria = example.createCriteria();
		criteria.andOnlyKeyEqualTo(commkeyId);
		List<Commkey> commKeyList = this.commkeyReadMapper.selectByExample(example);
		return commKeyList;

	}

	@Override
	public Integer getRegionByList(Integer code, List<Commkey> commKeyList) {
		Integer provinceId = null;
		Integer cityId = null;
		Integer areaId = null;
		for (int i = 0; i < commKeyList.size(); i++) {

			switch (code) {
			case 1:
				if (commKeyList.get(i).getCommonKey().equals(RegionEnum.PROVINCE.getDesc())) {
					provinceId = commKeyList.get(i).getCommonValue();
					return provinceId;
				}
			case 2:
				if (commKeyList.get(i).getCommonKey().equals(RegionEnum.CITY.getDesc())) {
					cityId = commKeyList.get(i).getCommonValue();
					return cityId;
				}
			case 3:
				if (commKeyList.get(i).getCommonKey().equals(RegionEnum.AREA.getDesc())) {
					areaId = commKeyList.get(i).getCommonValue();
					return areaId;
				}
			}

		}
		return null;
	}

	@Override
	public Long insertCommkey(List<Commkey> commkeyList) {
		return this.commkeyWriteMapper.betchInsertCommKey(commkeyList);
	}

}
